package kr.hkit.powerjava.chapter17.work;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class CheckboxTest02 extends JPanel implements ItemListener {
	private JPanel panel;
	JCheckBox[] btn = new JCheckBox[3];
	String[] img = {"kimtaehee", "kimtaehee2", "kimtaehee3"};
	JLabel[] piclbl = new JLabel[3];
	ImageIcon[] imgIcon = new ImageIcon[3];
	
	public CheckboxTest02() {

		initialize();
	}
	private void initialize() {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setLayout(new GridLayout(0,4));
		
		for(int i=0; i<3;i++){
			btn[i] = new JCheckBox(img[i]);
			btn[i].addItemListener(this);
			piclbl[i] = new JLabel(img[i]+".jpg");
			imgIcon[i] = new ImageIcon(img[i]+".jpg");
			add(piclbl[i]);
	}
		JPanel checkPanel = new JPanel(new GridLayout(0,1));
			for(int i=0; i<3 ; i++){
				checkPanel.add(btn[i]);
			}
		
		add(checkPanel);
		add(piclbl[0]);
		add(piclbl[1]);
		add(piclbl[2]);
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		ImageIcon img = null;
		
		Object src = e.getItemSelectable();							 //선택된 체크박스를 얻는다..
		for(int i=0; i<3; i++){
			if(src == btn[i]){
				if(e.getStateChange() == ItemEvent.DESELECTED){		//체크가 해제되면...
					piclbl[i].setIcon(null);
				}else{
					piclbl[i].setIcon(imgIcon[i]);
					piclbl[i].setText("");
				}
			}
		}
	}
}
